app.controller("administratorCreateCtrl", ["$scope","sweetAlertUtil","AdministratorApi","$state","$timeout",
    function($scope,sweetAlertUtil,AdministratorApi,$state,$timeout) {

        var chooseIds = []

        // ---------------------start 初始化角色 -------------------------------
        initRole()
        function initRole(){
            AdministratorApi.getRole().then(function(res){
                if(res.code == 200){
                    $scope.role = res.result
                }
            },function(){})
        }
        // ---------------------end 初始化角色 -------------------------------


        // ---------------------start 初始化城市 -------------------------------
        initCity()
        function initCity(){
            AdministratorApi.getCity().then(function(res){
                if(res.code == 200){
                    $scope.city = res.result.list
                    $timeout(function(){
                        $scope.item = "C51"
                    },1000)
                    $timeout(function(){
                        $scope.item_1 = "C510100000000"
                    },1000)
                }
            },function(){})
        }
        $scope.$watch('item',function(value){
            if($scope.item == undefined){
                return
            }
            AdministratorApi.getCity({parentId:$scope.item}).then(function(res){
                if(res.code == 200){
                    $scope.city_1=res.result.list
                }
            },function(){})
            $scope.item_1=null;
            $scope.city_2 = []
            

        });
        $scope.$watch('item_1',function(value){
            if($scope.item_1 == undefined){
                return
            }
            chooseIds = []
            AdministratorApi.getCity({parentId:$scope.item_1}).then(function(res){
                if(res.code == 200){
                    $scope.city_2=res.result.list
                }
            },function(){})

        });

        $scope.checkeSelect = function(item) {
            if (item._checked == true) {
                chooseIds.push(item.city_id);
            } else {
                chooseIds.splice(chooseIds.indexOf(item.city_id), 1);
            }
        }
        // ---------------------end 初始化城市 -------------------------------


        // ---------------------start 提交 -------------------------------
        $scope.submit = function(){
            if($scope.name==undefined || $scope.name==""){
                sweetAlertUtil.fail("输入名字不能为空！");
                return
            }
            if($scope.rid == undefined){
                sweetAlertUtil.fail("管理员不能为空！");
                return
            }
            // var regPwd = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/;

            if($scope.phone.length!=11){
                sweetAlertUtil.fail("输入手机号不正确！");
                return
            }

            if($scope.pwd.length>16 || $scope.pwd.length<6){
                sweetAlertUtil.fail("密码长度不对！请重新输入");
                return
            }

            if($scope.pwd != $scope.Cpwd){
                sweetAlertUtil.fail("两次输入密码不相同,请重新输入！");
                return
            }

            if($scope.rid == 2){
                if(chooseIds.length == 0){
                    sweetAlertUtil.fail("县/区不能为空！");
                    return
                }
                var params = {
                    name:$scope.name,
                    phone:$scope.phone,
                    roleId:$scope.rid,
                    password:$scope.pwd,
                    cityIds:chooseIds.join(",")
                }   
            }else{
                var params = {
                    name:$scope.name,
                    phone:$scope.phone,
                    roleId:$scope.rid,
                    password:$scope.pwd
                }   
            }
            console.log(params)
            sweetAlertUtil.confirm("确认保存管理员信息?", function(isconfirm) {
                if (isconfirm) {
                    AdministratorApi.getAdministratorCreate(params).then(function(res){
                        if(res.code == 200){
                            sweetAlertUtil.success("保存成功！","确定",function(){
                                $state.go("app.administrator.list")
                            })     
                        }else{
                            sweetAlertUtil.fail(res.msg);
                        }
                    },function(){})
                }

            })

        }
        // ---------------------end 提交 -------------------------------

    }
]);